package com.example.lancooznbk.view.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.lancooznbk.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by tangminglong on 17/10/16.
 */

public class QuesIndexView extends FrameLayout{

  private Context mContext;
  private Button btnIndex;
  private ImageView imgIndex;
  private LinearLayout linerIndex;

    public static final int STATUS_NO_ANSWER = 1;//未作答
    public static final int STATUS_RIGHT = 2;
    public static final int STATUS_WRONG = 3;
    public static final int STATUS_ANSWER = 4;

    private int mQuesIndexStatus = STATUS_NO_ANSWER;

    public QuesIndexView(Context context) {
        this(context,null);
    }

    public QuesIndexView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        View.inflate(mContext, R.layout.layout_ques_index,this);
        btnIndex = (Button) findViewById(R.id.btn_index);
        imgIndex = (ImageView) findViewById(R.id.img_index);
        linerIndex  = (LinearLayout) findViewById(R.id.liner_index);
        imgIndex.setVisibility(INVISIBLE);
        btnIndex.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mQuesIndexClickListener != null) {
                    imgIndex.setVisibility(VISIBLE);
                    mQuesIndexClickListener.onClick(v);
                }
            }
        });

    }

    /**
     * 设置文本内容
     * @param text
     */
    public void setQuesIndexText(String text){
        btnIndex.setText(text);
    }

    /**
     * 设置状态
     * @param quesIndexStatus
     */
    public void setQuesIndexStatus(@QuesIndexStatus int quesIndexStatus){
       this.mQuesIndexStatus = quesIndexStatus;
        _switchQuesIndexView();
    }

    private void _switchQuesIndexView() {
        switch (mQuesIndexStatus){
            case STATUS_NO_ANSWER:
                btnIndex.setBackgroundResource(R.mipmap.round_white);
                btnIndex.setTextColor(getResources().getColor(R.color.blue));
                break;
            case STATUS_RIGHT:
                btnIndex.setBackgroundResource(R.mipmap.round_green);
                btnIndex.setTextColor(getResources().getColor(R.color.white));
                break;
            case STATUS_WRONG:
                btnIndex.setBackgroundResource(R.mipmap.round_red);
                btnIndex.setTextColor(getResources().getColor(R.color.white));
                break;
            case STATUS_ANSWER:
                btnIndex.setBackgroundResource(R.mipmap.round_blue);
                btnIndex.setTextColor(getResources().getColor(R.color.white));
                break;
        }
    }



    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STATUS_NO_ANSWER, STATUS_RIGHT, STATUS_WRONG,STATUS_ANSWER})
     @interface QuesIndexStatus{}

    private QuesIndexClickListener mQuesIndexClickListener;

    public void setQuesIndexClickListener(QuesIndexClickListener quesIndexClickListener){
        this.mQuesIndexClickListener = quesIndexClickListener;
    }

    public interface QuesIndexClickListener{
        void onClick(View v);
    }

    public void setQuesIndexChecked(boolean checked){
        imgIndex.setVisibility(checked ? VISIBLE : INVISIBLE);
    }
}
